package com.yspa.activityNotice.controller;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hsnn.medstgmini.util.Pagination;
import com.yspa.activityNotice.model.form.ActivityNoticeForm;
import com.yspa.activityNotice.service.ActivityNoticeManager;
import com.yspa.knowledeUser.model.form.KnowledeUserForm;
/**
 * 活动公告
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/activityNotice")
public class ActivityNoticeController {

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private ActivityNoticeManager manager;
	/**
	 * 跳向活动公告列表页面
	 * @return
	 */
	@RequestMapping("/toActivityNoticeList")
	public String toActivityNoticeList(){
		return "/yspa/activityNotice/activityNoticeList";
	}
	/**
	 * 获取公告列表数据
	 * @return
	 */
	@RequestMapping("/getActivityNoticeList")
	@ResponseBody
	public Pagination getActivityNoticeList(){
		Pagination page = new Pagination(request);
		page.getConditions().put("title", request.getParameter("title"));
		manager.getActivityNoticeList(page);
		return page;
	}
	/**
	 * 新增活动公告
	 * @param form
	 * @param img
	 * @return
	 */
	@RequestMapping("/addActivityNotice")
	@ResponseBody
	public Pagination addActivityNotice(ActivityNoticeForm form,MultipartFile img){
		Pagination page = new Pagination();
		try {
			manager.addActivityNotice(img, form);
			page.setSuccess(true);
		} catch (Exception e) {
			page.setSuccess(false);
			page.setMsg(e.getMessage());
		}
		return page;
	}
	/**
	 * 获取活动公告信息
	 * @return
	 */
	@RequestMapping("/getEditInfo")
	@ResponseBody
	public Pagination getEditInfo(){
		String id = request.getParameter("id");
		Map<String, Object> obj = manager.getEditInfo(id);
		Pagination page = new Pagination();
		page.setRows(obj);
		return page;
	}
	/**
	 * 编辑活动公告信息
	 * @param form
	 * @param img
	 * @return
	 */
	@RequestMapping("/editInfo")
	@ResponseBody
	public Pagination editInfo(ActivityNoticeForm form,MultipartFile img){
		Pagination page = new Pagination();
		try {
			manager.editInfo(form, img);
			page.setSuccess(true);
		} catch (Exception e) {
			page.setSuccess(false);
			page.setMsg(e.getMessage());
		}
		return page;
	}
	/**
	 * 删除活动公告
	 * @param array
	 * @return
	 */
	@RequestMapping("/deleteUser")
	@ResponseBody
	public Pagination deleteUser(String array){
		Pagination page = new Pagination();
		try {
			if (array != null && !"".equals(array)) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<KnowledeUserForm>>() {
				}.getType();
				List<KnowledeUserForm> list = gson.fromJson(array, type);
				if (list != null && list.size()>0) {
					List<String> plist = new ArrayList<String>();
					for (int i = 0; i < list.size(); i++){
						plist.add(list.get(i).getId());
					}
					manager.deleteUser(plist);
					page.setSuccess(true);
				}
			}
		} catch (Exception e) {
			page.setSuccess(false);
		}
		return page;
	}
}
